package lab;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import javax.swing.*;

public class SouthPanel extends JPanel {

    JButton previous = new JButton("Previous");
    JButton checkout = new JButton("Checkout");

    Cards cards = new Cards(null);
    private ButtonPressed buttonPressed = new ButtonPressed();
    GridBagLayout gridBagLayout;
    GridBagConstraints c = new GridBagConstraints();

    public SouthPanel(Cards cards) {
        this.cards = cards;
        gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);
        c.weightx = 6;
        c.weighty = 0;
        c.gridx = 1;
        c.gridy = 0;
        add(previous, c);
        
        c.fill = GridBagConstraints.EAST;
        c.weightx = 0.5;
        c.weighty = 0;
        c.gridwidth = 2;
        c.gridx = 3;
        c.gridy = 0;
        add(checkout, c);
        gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);
        //c.fill=GridBagConstraints.EAST;
        c.weightx = 6;
        c.weighty = 0;
        c.gridx = 1;
        c.gridy = 0;
        add(previous,c);
        
        c.fill=GridBagConstraints.EAST;
        c.weightx = 0.5;
        c.weighty = 0;
        c.gridwidth=2;
        c.gridx = 3;
        c.gridy = 0;
        add(checkout,c);
        previous.addActionListener(buttonPressed);
        checkout.addActionListener(buttonPressed);

    }

    private class ButtonPressed implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String actionCommand = e.getActionCommand().toString();
            if (actionCommand.equals("Previous")) {
                cards.cl.show(cards.cardPanel, "1");
            } else if (actionCommand.equals("Checkout")) {
                try {
                    String ISBN = cards.centerPanel.getISBN();
                    cards.centerPanelRear.arrayListISBN.add(ISBN);
                    cards.centerPanel.database.updateStock(ISBN);
                    
                } catch (Exception ex) {
                    System.out.println("Could not update stock");
                }
            }
        }
    }
}
